//
//  PresidentsViewController.m
//  Nav
//
//  Created by KaiKai on 11-8-10.
//  Copyright 2011年 SDS. All rights reserved.
//

#import "PresidentsViewController.h"
#import "PresidentDetailController.h"
#import "President.h"
#import "NavAppDelegate.h"

@implementation PresidentsViewController
@synthesize list;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
	[list release];
	[super dealloc];
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
	
	NSString *path = [[NSBundle mainBundle] pathForResource:@"Presidents" ofType:@"plist"];
	
	NSData *data;
	NSKeyedUnarchiver *unarchiver;
	data = [[NSData alloc] initWithContentsOfFile:path];
	unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
	
	NSMutableArray *array = [unarchiver decodeObjectForKey:@"Presidents"];
	self.list = array;
	[unarchiver finishDecoding];
	[unarchiver release];
	[data release];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *PresidentListCellIdentifier = @"PresidentListCellIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PresidentListCellIdentifier];
	if (cell == nil)
	{
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:PresidentListCellIdentifier] autorelease];
	}
	NSUInteger row = [indexPath row];
	President *thePres = [list objectAtIndex:row];
	cell.text = thePres.name;
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSUInteger row = [indexPath row];
	President *prez = [list objectAtIndex:row];
	
	PresidentDetailController *childController = [[PresidentDetailController alloc] initWithStyle:UITableViewStyleGrouped];
	
	childController.title = prez.name;
	childController.president = prez;
	
	NavAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	UINavigationController *navController = [delegate navController];
	[navController pushViewController:childController animated:YES];
	[childController release];
}

@end
